<script setup>
import { ref } from 'vue'
import { useUserStore } from '@/stores/modules/user.js'
import { useRoute, useRouter } from 'vue-router'
import Footer from './components/Footer.vue'
import Nav from './components/Nav.vue'
import { useCartStore } from '@/stores/modules/cart'

const loginForm = ref({
  account: '',
  password: '',
  type: [],
})
const route = useRoute()
const router = useRouter()
const form = ref()
const cartStore = useCartStore()

const userInfo = useUserStore()
const loginSubmit = async () => {
  await form.value.validate()
  await userInfo.getUserInfo(loginForm.value)
  cartStore.mergeCart()
  const redirectPath = route.query.redirect || '/'
  router.push(redirectPath)
}

const rules = {
  account: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
  type: [{ required: true, message: '请勾选协议', trigger: 'blur' }],
}
</script>
<template>
  <Nav class="header"></Nav>
  <div class="login">
    <div class="login-form">
      <h1>账户登录</h1>
      <el-form ref="form" :rules="rules" :model="loginForm" label-width="auto">
        <el-form-item label="用户名" prop="account">
          <el-input v-model="loginForm.account" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码：" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        <el-form-item prop="type">
          <el-checkbox-group v-model="loginForm.type">
            <el-checkbox value="1" name="type"> 我已同意隐私条款和服务条款 </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="loginSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
  <Footer class="footer" />
</template>

<style scoped>
.login {
  width: 100vw;
  height: calc(100vh - 60px - 100px);
  position: relative;
  background-image: url('../../assets/login-bg.png');
  background-size: cover;

  .login-form {
    width: 380px;
    height: 320px;
    position: absolute;
    right: 460px;
    top: 100px;
    background-color: white;

    h1 {
      text-align: center;
    }

    .el-form {
      padding: 0 40px;
      margin-top: 40px;

      .btn {
        width: 100%;
      }
    }
  }
}

.footer {
  background-color: white;
}
</style>
